//
//  HeaderView.m
//  QQGroup
//
//  Created by cocoazxj@163.com on 15/3/31.
//  Copyright (c) 2015年 handcool. All rights reserved.
//

#import "HeaderView.h"

@interface HeaderView()
@property (nonatomic, strong) UIButton *sectionName;
@property (nonatomic, strong) UILabel *online;
@end

@implementation HeaderView
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithReuseIdentifier:reuseIdentifier])
    {
        UIButton *sectionName = [UIButton buttonWithType:UIButtonTypeCustom];
        [sectionName setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [sectionName setImage:[UIImage imageNamed:@"buddy_header_arrow"] forState:UIControlStateNormal];
        [sectionName setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg"] forState:UIControlStateNormal];
        [sectionName setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
        [sectionName setContentEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
        [sectionName setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
        [sectionName addTarget:self action:@selector(expansionButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [sectionName.imageView setContentMode:UIViewContentModeCenter];
        [sectionName.imageView.layer setMasksToBounds:NO];
        [sectionName.imageView setClipsToBounds:NO];
        [self addSubview:sectionName];
        self.sectionName = sectionName;
        UILabel *online = [[UILabel alloc] init];
        [online setFont:[UIFont systemFontOfSize:14]];
        [online setTextAlignment:NSTextAlignmentRight];
        [self addSubview:online];
        self.online = online;
    }
    return self;
}

+ (instancetype)headerView:(UITableView *)tableView
{
    static NSString *identi = @"identi";
    HeaderView *head = [tableView dequeueReusableCellWithIdentifier:identi];
    if(!head)
    {
        head = [[HeaderView alloc] initWithReuseIdentifier:identi];
    }
    return head;
}

- (void)layoutSubviews
{
//    NSLog(@"layoutSubviews");
    [super layoutSubviews];
    self.sectionName.frame = self.bounds;
    self.sectionName.backgroundColor = [UIColor clearColor];
    self.online.frame = CGRectMake(230, 0, 80, 44);
}

- (void)didMoveToSuperview
{
//    NSLog(@"didMoveToSuperview");
    if(self.groupModel.isOpen)
    {
        self.sectionName.imageView.transform = CGAffineTransformMakeRotation(M_PI_2);
    }
}

- (void)willMoveToSuperview:(UIView *)newSuperview
{
//    NSLog(@"willMoveToSuperview");
}

- (void)setGroupModel:(GroupModel *)groupModel
{
    _groupModel = groupModel;
    [self.sectionName setTitle:groupModel.name forState:UIControlStateNormal];
    [self.online setText:[NSString stringWithFormat:@"在线%d/%d",groupModel.online,groupModel.count]];
}

- (IBAction)expansionButtonClicked:(UIButton *)btn
{
    self.groupModel.Open = !self.groupModel.isOpen;
    [self.delegate headerViewDelegate];
}
@end
